import { defineStore } from 'pinia'

interface MockService {
  id: string
  name: string
  url: string  // API路径，例如: /v1/mocs/1
  method: string  // HTTP方法: GET, POST, PUT, DELETE
  response: string  // 响应内容，JSON格式
}

interface MockState {
  activeService: MockService | null
  services: MockService[]
  requestHistory: any[]
}

export const useMockStore = defineStore('mock', {
  state: (): MockState => ({
    activeService: null,
    services: [],
    requestHistory: []
  }),
  
  actions: {
    setActiveService(service: MockService) {
      this.activeService = service
    },
    
    addService(service: MockService) {
      this.services.push(service)
    },
    
    updateService(updatedService: MockService) {
      const index = this.services.findIndex(s => s.id === updatedService.id)
      if (index !== -1) {
        this.services[index] = updatedService
      }
    },
    
    removeService(id: string) {
      this.services = this.services.filter(s => s.id !== id)
    },
    
    addRequestToHistory(request: any) {
      this.requestHistory.push(request)
    }
  },
  
  getters: {
    getServiceById: (state) => (id: string) => {
      return state.services.find(s => s.id === id)
    }
  }
})